import 'package:flutter_yishop/constants/Constants.dart';
import 'package:sp_util/sp_util.dart';

class SearchServices {
  static setHistoryData(String keywords) {
    /*
          1、获取本地存储里面的数据  (searchList)

          2、判断本地存储是否有数据

              2.1、如果有数据

                    1、读取本地存储的数据
                    2、判断本地存储中有没有当前数据，
                        如果有不做操作、
                        如果没有当前数据,本地存储的数据和当前数据拼接后重新写入


              2.2、如果没有数据

                    直接把当前数据放在数组中写入到本地存储


      */

    try {
      List<String> searchListData = SpUtil.getStringList(Constants.SEARCHLIST);
      print(searchListData);
      var hasData = searchListData.any((element) => element == keywords);
      if (!hasData) {
        searchListData.add(keywords);
        SpUtil.putStringList(Constants.SEARCHLIST, searchListData);
      }
      Map searchListMap = SpUtil.getObject(Constants.SEARCHLISTMAP);
      if(searchListMap.containsKey(keywords)){
        int value = searchListMap[keywords] + 1;
        searchListMap[keywords] = value;
      }else{
        searchListMap[keywords] = 1;
      }
      SpUtil.putObject(Constants.SEARCHLISTMAP, searchListMap);


    } catch (e) {
      List<String> tempList = new List();
      tempList.add(keywords);
      SpUtil.putStringList(Constants.SEARCHLIST, tempList);

      Map map = Map();
      map[keywords] = 1;
      SpUtil.putObject(Constants.SEARCHLISTMAP, map);

    }

  }

  static List getHotSearchList(){
    int hotSearchNum =4;//热搜最多个数
    try{
      Map mmap = SpUtil.getObject(Constants.SEARCHLISTMAP);
      List hotSearchList = List();
      if(mmap!=null&&mmap.length>0){
        if(mmap.length<=hotSearchNum){
          mmap.forEach((key, value) {
            // print('key:$key  value:$value');
            hotSearchList.add(key);

          });
        }else{
          var valueList= List();
          var keyList= List();
          mmap.forEach((key, value) {
            // print('key:$key  value:$value');
            keyList.add(key);
            valueList.add(value);

          });
          ///冒泡排序
          for (var i = 0; i < valueList.length-1; i++) {
            for (var j = 0; j < valueList.length-1-i; j++) {
              if(valueList[j]>valueList[j+1]){
                int temp=valueList[j];
                valueList[j]=valueList[j+1];
                valueList[j+1]=temp;
                String keyTemp = keyList[j];
                keyList[j] = keyList[j+1];
                keyList[j+1] = keyTemp;
              }
            }
          }
          hotSearchList = keyList.sublist(0,hotSearchNum);
          print('${hotSearchList.reversed}');
        }
      }
      return hotSearchList;
    }catch(e){
      return [];
    }

  }


  static List getHistoryList() {
    try {
      List searchListData = SpUtil.getStringList(Constants.SEARCHLIST);
      return searchListData;
    } catch (e) {
    return [];
    }
  }

  static clearHistroyList(keywords) {
    List searchListData = SpUtil.getStringList(Constants.SEARCHLIST);
    searchListData.remove(keywords);
    SpUtil.putStringList(Constants.SEARCHLIST, searchListData);
  }

  static clearAllHistroy() {
    List searchListData = SpUtil.getStringList(Constants.SEARCHLIST);
    searchListData.clear();
    SpUtil.putStringList(Constants.SEARCHLIST, searchListData);
  }
}
